package com.starblink.android.basic.widget.edittext

import com.starblink.android.basic.widget.edittext.ClearEditText
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow


// 构建输入框文字变化流
fun ClearEditText.textChangeFlow(): Flow<String> = callbackFlow {
    // 构建输入框监听器
    val watcher = object: ClearEditText.OnEditInputListener {
        override fun input(content: String){
            trySend(content)
        }
    }
    // 设置输入框监听器
    setOnEditInputListener(watcher)
    awaitClose { setOnEditInputListener(null) } // 阻塞以保证流一直运行
}